<?php

namespace app\index\controller;

use Common;
use think\Db;

class Branch extends Common
{
    public function getBranches()
    {
        $total = 0;
        $list = [];
        if (isset($_GET["page"]) && isset($_GET["limit"])) {
            $list = db("branch")->page($_GET["page"], $_GET["limit"])->field("bid as id, name")->select();
            $total = db("branch")->count();
        }

        $array = [
            "list" => $list,
            "total" => $total
        ];

        $this->Code(200, false, $array);
    }

    public function updateBranch()
    {
        if (isset($_POST["name"]) && $_POST["name"] != "") {
            $info = [
                "name" => $_POST["name"]
            ];
            if (isset($_POST["id"])) {
                $check = db("branch")->where("bid != {$_POST["id"]}")->where("name", $_POST["name"])->find();
                if (empty($check)) {
                    db("branch")->where("bid", $_POST["id"])->update($info);
                    $msg = "修改成功";
                } else {
                    $this->Code(403, "名称重复", (object)[]);
                }
            } else {
                $check = db("branch")->where("name", $_POST["name"])->find();
                if (empty($check)) {
                    db("branch")->insert($info);
                    $msg = "添加成功";
                } else {
                    $this->Code(403, "名称重复", (object)[]);
                }
            }
            $this->Code(200, $msg, (object)[]);
        }
    }

    public function getAllBranches()
    {
        $array = db("branch")->field("bid as id, name")->select();
        $this->Code(200, false, $array);
    }
    /**
     * 获取有权限的部门-每日报表，月报统计用，只显示有权限的部门
     */
    public function getAuthBranches()
    {
        $aid = $this->token["aid"];
        $authBranchIds = db("admin")->where('aid',$aid)->value('authBranchIds');
        $array = db("branch")->field("bid as id, name")->where([['bid','in',$authBranchIds]])->select();
        $this->Code(200, false, $array);
    }

    public function deleteBranch()
    {
        if (isset($_POST["id"])) {
            db("branch")->where("bid", $_POST["id"])->delete();
            $info = [
                "bid" => 0
            ];
            db("admin")->where("bid", $_POST["id"])->update($info);
        }
        $this->Code(200, "删除成功", (object)[]);
    }

    public function getAllBranchesByTree()
    {
        $arr1 = Db::table("m_statistics.s_branch")->where("belong", 1)->where("is_del", 0)->field("bid as id, concat(name, ' - ', code) as name")->select();
        $arr2 = Db::table("m_statistics.s_branch")->where("belong", 2)->where("is_del", 0)->field("bid as id, concat(name, ' - ', code) as name")->select();
        $array = [
            [
                "id" => 1,
                "name" => $this->Language("线上"),
                "children" => $arr1
            ],
            [
                "id" => 2,
                "name" => $this->Language("线下"),
                "children" => $arr2
            ]
        ];
        $this->Code(200, false, $array);
    }
}
